package com.swdegao.api.util;

import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;


/**
 * 
 * @author 只会走路的小鸟
 * 2020-11-05
 *
 */
/*@Component
@Repository
@SuppressWarnings({"unchecked","rawtypes"})*/
    @Service
public class RocketMQUtils {
	//定义一个全局的记录器，通过LoggerFactory获取
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
	
	public void sendMessage(String topic, Object data) {
       rocketMQTemplate.convertAndSend(topic, data);
    }

    public void sendMessage(String topic, String tags, Object data) {
        rocketMQTemplate.convertAndSend(String.format("%s:%s", topic, tags), data);
    }
    public void sendMessageOfKeys(String keys, String topic, Object data) {
    	Message message = MessageBuilder.withPayload(data).setHeader("KEYS", keys).build();
   	    rocketMQTemplate.convertAndSend(topic, message);
   }
   public void sendMessage(String keys, String topic,String tags, Object data) {
    	Message message = MessageBuilder.withPayload(data).setHeader("KEYS", keys).build();
    	 //rocketMQTemplate.convertAndSend(String.format("%s:%s", topic, tags), message);
   	    rocketMQTemplate.syncSend(String.format("%s:%s", topic, tags), message);
   }

}